# -*- coding: utf-8 -*-
from typing import Optional

import lxml
from lxml import etree

class HtmlClient:

    def find_element(self, html: Optional[str] = None, xpath_exp: Optional[str] = None,index=0):
        elements = self.find_elements(html, xpath_exp)
        if elements == None or len(elements) == 0:
            return None
        return elements[index]

    def find_element_str(self, html: Optional[str] = None, xpath_exp: Optional[str] = None, index=0):
        elements = self.find_elements(html, xpath_exp)
        if elements == None or len(elements) == 0:
            return None
        return self.to_str(elements[index])

    def find_elements(self, html : Optional[str] = None, xpath_exp: Optional[str] = None):
        if html == None or len(html) == 0:
            return None
        htmlNode = etree.HTML(html)
        elements = htmlNode.xpath(xpath_exp)
        return elements

    def find_elements_str(self, html: Optional[str] = None, xpath_exp: Optional[str] = None):
        elements = self.find_elements(html, xpath_exp)
        if elements == None or len(elements) == 0:
            return None
        return self.to_str(elements)

    def to_str(self,element):
        if isinstance(element, list):
            elementStrs = [etree.tostring(x, encoding='utf-8').decode('utf-8') for x in element]
            return "\n".join(elementStrs)
        elementStr = etree.tostring(element, encoding='utf-8').decode('utf-8')
        return elementStr

    def find_urls(self,html,xpath,website=None):
        elements = self.find_elements(html, xpath)
        urls = [element.attrib.get('href') for element in elements if element.attrib.get('href') != None]
        if website==None:
            return urls
        result=[]
        for url in urls:
            if url.startswith(website):
                result.append(url)
            else:
                result.append(website+url)
        return result

    def get_index_of_element(self, elements, contain_str=None):
        if contain_str == None:
            raise ValueError("contain_str不能为空")
        for index, element in enumerate(elements):
            element_str = self.to_str(element)
            if contain_str in element_str:
                return index
        return 99999

    def get_index_of_element_by_searchkey(self, elements, contain_strs=None):
        if contain_strs == None:
            raise ValueError("contain_str不能为空")
        for index, element in enumerate(elements):
            element_str = self.to_str(element)
            flag=True
            for i in contain_strs:
                if i not in element_str:
                    flag=True
                    break
        return 99999

    def extarct_view_text(self,html):
        if isinstance(html, lxml.etree._Element):
            html=self.to_str(html)
        htmlNode = etree.HTML(html)
        texts = htmlNode.xpath('//text()[normalize-space()]')
        if texts==None or len(texts)==0:
            return ""
        return "\n".join(texts)





